package com.jxnu.controller;

import com.jxnu.domain.User;
import com.jxnu.domain.Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value="user")
public class TestController {

    @RequestMapping("/save15")
    @ResponseBody
    public void test15(String username, MultipartFile upload, HttpServletRequest request) throws IOException {
        System.out.println(username);
        String fileName = upload.getOriginalFilename();
        String realPath = request.getServletContext().getRealPath("/upload/");
        File file = new File(realPath);
        if(!file.exists()){
            file.mkdir();
        }
        System.out.println(realPath);
        System.out.println(realPath+fileName);
        upload.transferTo(new File(realPath+fileName));
    }

    @RequestMapping("/save13")
    public String test13(HttpServletResponse resp) {


        Cookie cookie = new Cookie("ck","tom");

        resp.addCookie(cookie);

        return "success";

    }

    @RequestMapping("/save14")
    @ResponseBody
    public String test14(@CookieValue String ck) {
        System.out.println(ck);

        return "a";

    }

    @RequestMapping(value = "save12")
    public String save12(Model model){   //
        model.addAttribute("username","zhangsan");
//        return "success";
//        mv.addAttribute("name","zhangsan");
        return "redirect:http://localhost:8080/jsp/success.jsp";
    }

    @RequestMapping(value = "save11/{name}/{age}")
    @ResponseBody
    public void save11(@PathVariable String name, @PathVariable int age){   //
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping(value = "save10")
    @ResponseBody
    public void save10(Date date){   //
        System.out.println(date);
    }

    @RequestMapping(value = "save9")
    @ResponseBody           //@RequestParam(value = "name",required = false,defaultValue = "lucy") 默认值lucy
    public void save9(@RequestParam(value = "name",required = false,defaultValue = "lucy") String username){   //
        System.out.println(username);
    }

    @RequestMapping(value = "save8")
    @ResponseBody
    public void save8(@RequestBody List<User> list){   //
        list.forEach(System.out::println);
    }

    @RequestMapping(value = "save7")
    @ResponseBody
    public void save7(Vo vo){   //
        System.out.println(vo);
    }

    @RequestMapping(value = "save6")
    @ResponseBody
    public void save6(String [] subject){   //  请求参数的字段名称与对象的属性同名时  可以直接封装到对象上
        for (int i = 0; i < subject.length; i++) {
            System.out.println(subject[i]);
        }
    }

    @RequestMapping(value = "save5")
    @ResponseBody
    public void save5(User user){   //  请求参数的字段名称与对象的属性同名时  可以直接封装到对象上
        System.out.println(user);
    }

    @RequestMapping(value = "save4")
    @ResponseBody
    public void save4(String name, int age){   //回写数据
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping(value = "save3")
    @ResponseBody
    public User save3(){   //回写数据
        User user = new User();
        user.setName("zhangsan");
        user.setAge(20);
        return user;
}

    @RequestMapping(value = "save2")
    @ResponseBody
    public String save2(){   //回写数据
        return "Hello World";
    }

    @RequestMapping(value = "save1")
    public ModelAndView save1(){        //页面跳转携带数据
            ModelAndView mv=new ModelAndView();
            mv.setViewName("success");
            mv.addObject("username","zhangsan");
            return mv;
        }
}
